﻿using Hospital.IService;
using Hospital.Model.entity;
using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hospital.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    //预约挂号
    public class RegistrationController : ControllerBase
    {
        private readonly IRegistrationService _iRegistrationService;
        public RegistrationController(IRegistrationService iRegistrationService)
        {
            _iRegistrationService = iRegistrationService;
        }
        [HttpPost]
        public async Task<ActionResult<ApiResult>> PostRegistration([FromBody] Registration registration)
        {
            try
            {
                bool result = await _iRegistrationService.CreateAsync(registration);
                if (result) return ApiResultHelper.Success(result);
                return ApiResultHelper.Error("添加失败");
            }
            catch (Exception ex)
            {
                return BadRequest(new { Error = ex.Message });
                throw;
            }
        }

        [HttpGet]
        public async Task<ActionResult<ApiResult>> GetRegistrations()
        {
            try
            {
                var registrations = await _iRegistrationService.QueryAsync();
                return ApiResultHelper.Success(registrations);
            }
            catch (Exception ex)
            {
                return BadRequest(new { Error = ex.Message });
                throw;
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResult>> DeleteRegistration(int id)
        {
            try
            {
                bool b = await _iRegistrationService.DeleteAsync(id);
                if (b) return ApiResultHelper.Success(b);
                return ApiResultHelper.Error("error");
            }
            catch (Exception ex)
            {
                return BadRequest(new { Error = ex.Message });
                throw;
            }
        }
    }
}
